package com.crois.barrier.web.plugin.route;

import com.crois.barrier.common.constants.Constants;
import com.crois.barrier.common.dto.ApiRouteInfo;
import com.crois.barrier.common.enums.PluginEnum;
import com.crois.barrier.common.enums.PluginTypeEnum;
import com.crois.barrier.common.enums.UpstreamStrategyEnum;
import com.crois.barrier.common.utils.TimeUtil;
import com.crois.barrier.plugin.BarrierPlugin;
import com.crois.barrier.plugin.BarrierPluginChain;
import com.crois.barrier.web.loadbalance.strategy.DivideStrategy;
import com.crois.barrier.web.support.ServerWebExchangeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author Hou Ze Yu
 * @description 根据route 负载均衡客户点插件
 * @date 2020/7/13
 */
public class RouteLoadbalancerClientPlugin implements BarrierPlugin {

    Logger LOGGER = LoggerFactory.getLogger(RouteLoadbalancerClientPlugin.class);

    private DivideStrategy ipListDivideStrategy;

    private DivideStrategy serviceNameDivideStrategy;


    public RouteLoadbalancerClientPlugin(DivideStrategy ipListDivideStrategy, DivideStrategy serviceNameDivideStrategy) {
        this.ipListDivideStrategy = ipListDivideStrategy;
        this.serviceNameDivideStrategy = serviceNameDivideStrategy;
    }

    @Override
    public Mono<Void> execute(ServerWebExchange exchange, BarrierPluginChain chain) {

        ApiRouteInfo apiRouteInfo = exchange.getAttribute(ServerWebExchangeUtils.BARRIER_ROUTE_ATTR);

        // 设置 开始 时间
        exchange.getAttributes().put(Constants.REQUEST_START_TIME, TimeUtil.currentTimeMillis());

        LOGGER.info("[RouteLoadbalancerClient Plugin Start invoke]");

        if (apiRouteInfo==null){

            return chain.execute(exchange);

        }else{
            if (!apiRouteInfo.getUpstreamStrategy().equals(UpstreamStrategyEnum.URL_ROUTE.getName())){

                 // 如果 不是 url route 策略
                if (apiRouteInfo.getUpstreamStrategy().equals(UpstreamStrategyEnum.IP_LIST_ROUTE.getName())){

                      // 调用 ip_list_route 策略

                    ipListDivideStrategy.divide(apiRouteInfo,exchange);

                }else if (apiRouteInfo.getUpstreamStrategy().equals(UpstreamStrategyEnum.SERVICE_NAME_ROUTE.getName())){

                     // 调用 service_name_route 策略

                    serviceNameDivideStrategy.divide(apiRouteInfo,exchange);
                }
                LOGGER.info("[执行完成：RouteLoadbalancerClient]");
                return chain.execute(exchange);

            }else {
                LOGGER.info("[执行完成：RouteLoadbalancerClient]");
                return chain.execute(exchange);
            }
        }
    }

    @Override
    public PluginTypeEnum pluginType() {
        return PluginTypeEnum.ROUTE;
    }

    @Override
    public String name() {
        return PluginEnum.ROUTE_LOADBALANCER_CLIENT.getName();
    }

    @Override
    public int order() {
        return PluginEnum.ROUTE_LOADBALANCER_CLIENT.getCode();
    }

    @Override
    public Boolean skip(ServerWebExchange exchange) {
        return false;
    }
}
